<template>
  <el-dialog
    title="添加用户"
    :visible="addDialogVisible"
    width="30%"
    @close="addDialogClosed"
  >
    <!-- 表单区域 -->
    <el-form
      :model="addForm"
      :rules="addFormRules"
      ref="addFormRef"
      label-width="70px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="addForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="addForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="addForm.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="addForm.mobile"></el-input>
      </el-form-item>
    </el-form>
    <!-- 添加用户底部区域 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="addDialogClosed">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addUser } from '@/api/user'
export default {
  name: 'user-dialog',
  // 不是自己的，接受
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false
    }
  },
  // 自己的全部放在data里
  data() {
    // 因为这个是 dialog 弹窗需要的属性，所以需要一并抽离
    const checkEmail = (rule, value, callback) => {
      const regEmail = /^\w+@\w+(\.\w+)+$/
      if (regEmail.test(value)) {
        return callback()
      }
      callback(new Error('请输入合法的邮箱'))
    }
    const checkMobile = (rule, value, callback) => {
      const reMobile = /^1[34578]\d{9}$/
      if (reMobile.test(value)) {
        return callback()
      }
      callback(new Error('请输入合法的手机号码'))
    }
    return {
      // 弹窗的默认值、验证规则
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      },
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      }
    }
  },
  methods: {
    //     监听添加用户对话框的关闭事件
    addDialogClosed() {
      this.$emit('updateList')
      this.$refs.addFormRef.resetFields()
    },
    // 点击按钮新增新用户
    async confirm() {
      try {
        await this.$refs.addFormRef.validate()
        const res = await addUser(this.addForm)
        if (res.meta.status === 201) {
          this.$message.success('新增用户信息成功！')
          // 因为子组件不能直接更改父组件数据，所以下面的方法需要通知父组件更改
          // this.addDialogVisible = false
          // 加载列表
          // this.loadUserList()
          this.addDialogClosed()
          this.$emit('updateList')
        }
      } catch (error) {
        this.$message.error('新增用户信息失败!')
      }
    }
  }
}
</script>
<style lang="less" scope></style>
